import re
import os.path

def get_config(branch):
    class Config:
        filename_pattern = os.path.join(r"C:\Users\ezhechn\workspace\sqrdata\data\javatest\%s"%branch.name, "%s", "junit","overview-summary.html")
    return Config

def get_html(config, component, raw, old_raw, rank, old_rank):
    return str(raw)

def get_rank(config, component, raw):
    success,total = raw  
    if total == -1:
        return 0.0
    
    Y_point = 5.0 if success == total else 1.0

    loc = 2000  # no line of code for java components?
    X_point = 1.0
    steps = [(1, 0.1), (loc/200, 0.4), (loc/200+1, 0.6), (loc/100, 0.8), (loc/100+1, 1)]
    for step in steps:
        if total >= step[0]:
            X_point = step[1]
    
    return float(X_point * Y_point)

def get_raw(config, component):
    try:
        filename = config.filename_pattern % component.name
        f = open(filename)
        for aLine in f.readlines():
            #<td>229</td><td>0</td><td>0</td><td>100.00%</td><td>262.081</td>
            m = re.search('^<td>(\d+)</td><td>(\d+)</td><td>(\d+)</td><td>([0-9.]+%)</td><td>([0-9].+)</td>', aLine)
            if(m is not None):
                total = int(m.group(1))
                fail  = int(m.group(2))
                error = int(m.group(3))
                success = total - fail - error
                return success,total
    except IOError:
        return -1, -1
    return -1, -1

